package com.kuang.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.demo.bo.zzy.Log;
import com.kuang.demo.bo.zzy.LogBo.ListPageLimit2;
import com.kuang.demo.bo.zzy.LogExample;
import com.kuang.demo.bo.zzy.TotalItem;
import com.kuang.demo.mapper.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogServiceImpl implements LogService{
    @Autowired
    LogMapper logMapper;

    @Override
    public TotalItem selectLogList(ListPageLimit2 listPageLimit) {

        //排序首先
        LogExample logExample = new LogExample();
        logExample.setOrderByClause(listPageLimit.getSort() + " " + listPageLimit.getOrder());
        LogExample.Criteria criteria = logExample.createCriteria();

        //模糊查询加条件
        if(null != listPageLimit.getName()){
            criteria.andAdminLike(listPageLimit.getName());
        }

        //逻辑删除
        criteria.andDeletedEqualTo(false);

        //其次分页
        PageHelper.startPage(listPageLimit.getPage(),listPageLimit.getLimit());

        //查到数据
        List<Log> logs = logMapper.selectByExample(logExample);

        PageInfo pageInfo = new PageInfo(logs);
        int total = (int) pageInfo.getTotal();

        TotalItem totalItem = new TotalItem();
        totalItem.setTotal(total);
        totalItem.setItems(logs);

        return totalItem;
    }
}
